package com.renwen.sms.util;

import com.renwen.sms.exception.MyRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Objects;

/**
 * MultipartFile 转 File工具类
 */
@Slf4j
public class FileUtil {
    private FileUtil(){}
    static String path = null;

    /**
     * MultipartFile 转 File
     * @param file MultipartFile
     * @throws Exception e
     */
    public static void multipartFileToFile(MultipartFile file) throws Exception {
        if (file.getSize() > 0) {
            InputStream ins = file.getInputStream();
            File toFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
            inputStreamToFile(ins, toFile);
            ins.close();
        }else {
            throw new MyRuntimeException("上传文件有误...");
        }
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        generatePath("upload/");
        try(OutputStream os = new FileOutputStream("upload/"+file.getName())){
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            ins.close();
            os.flush();
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new MyRuntimeException(e.getMessage());
        }
    }

    /**
     * 删除本地临时文件
     *
     * @param file
     */
    public static Boolean deleteWordTempFile(XWPFDocument xwpfDocument, InputStream is, File file) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (xwpfDocument != null) {
            try {
                xwpfDocument.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (file != null) {
            File del = new File(file.toURI());
            return del.delete();
        } else {
            return false;
        }
    }

    /**
     * 删除本地临时文件
     *
     * @param file
     */
    public static Boolean deleteExcelTempFile(Workbook workbook, FileInputStream is, File file) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (workbook != null) {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (file != null) {
            File del = new File(file.toURI());
            return del.delete();
        } else {
            return false;
        }
    }

    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path  + tempList[i]);//先删除文件夹里面的文件
                flag = true;
            }
        }
        return flag;
    }

    public static void setTmpFilePath(String filePath) {
        path = filePath;
    }

    public static String getTmpFilePath() {
        return path;
    }

    /**
     * 生成路径
     * @param path
     */
    public static void generatePath(String path){
        File file = new File(path);
        if (!file.exists()){
            if (file.mkdirs()){
                log.info("path[{}] create success!", path);
            } else {
                log.error("path[{}] create failed!", path);
            }
        }
    }


}